﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace ShopBack.Web.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        // GET: User
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 用户信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public ActionResult GetUser(int limit, int page, string UserName,string Sex)
        {
            var total = 0;
            var list = UserService.GetUserMes(limit, page, UserName,Sex, out total).ToList();
            return Json(new { code = 0, msg = "", data = list, count = total }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 修改用户管理的页面
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult UserRole(int Id)
        {
            List<int> roleId = UserRoleService.GetRoleId(Id);
            StringBuilder stb = new StringBuilder();
            foreach (var item in roleId)
            {
                stb.Append(item + ",");
            }
            ViewBag.UserId = Id;
            ViewBag.RoleId = stb.ToString().TrimEnd(',');
            return View();
        }
        /// <summary>
        /// 获取角色数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetRole()
        {
            List<Role> list = RoleService.GetRoles();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult SetRoleData(string ids, string action, int userId)
        {

            int r = 0;
            if (action == "delRole")
            {
                r = UserRoleService.BatchDelete(ids, userId);
            }
            else
            {
                r = UserRoleService.BatchAdd(ids, userId);
            }
            if (r > 0)
            {
                return Json(new { Success = true, Message = "操作成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "操作失败" });
            }
        }


        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        /// <summary>
        /// 添加用户数据
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(string UserName,string PassWord,string Sex)
        {
            string Pwd = MD5Jm.Md5jm(PassWord);
            int a = UserService.GetAdd(UserName,Pwd,Sex);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "添加成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "添加失败" });
            }
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Update(int id)
        {
            User user = UserService.GetSelec(id);
            return View(user);
        }
        /// <summary>
        /// 获取编辑数据
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="RealName"></param>
        /// <param name="Age"></param>
        /// <param name="Mobile"></param>
        /// <param name="realId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Update(string UserName, string Sex, int realId)
        {
            int a = UserService.GetUpdate(UserName, Sex, realId);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "添加成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "添加失败" });
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(int id)
        {
            int a = UserService.GetDel(id);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public ActionResult BatchDelArticele(string Ids)
        {
            int a = UserService.GetBatchDelByIds(Ids);
            if (a > 0)
            {
                return Json(new { Success1 = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success1 = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 修改密码页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult UpdatePwd(int id)
        {
            User a = UserService.GetSelec(id);
            return View(a);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="PassWord">旧密码</param>
        /// <param name="NewPassWord">新密码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdatePwd(string UserName, string Pwd, string NewPwd)
        {

            User u = UserService.GetUser(UserName);
            string oldpwd = MD5Jm.Md5jm(Pwd);
            string newPwd = MD5Jm.Md5jm(NewPwd);
            if (u.Pwd == oldpwd)
            {
                if (string.IsNullOrEmpty(NewPwd))
                {
                    return Json(new { Success = false, Message = "新密码不能为空" });
                }
                else if (oldpwd == newPwd)
                {
                    return Json(new { Success = false, Message = "新密码和原密码不能一致" });
                }
                else
                {
                    int a = UserService.GetUpdatePwd(UserName, newPwd);
                    if (a > 0)
                    {
                        return Json(new { Success = true, Message = "修改成功" });
                    }
                    else
                    {
                        return Json(new { Success = false, Message = "修改失败" });
                    }
                }
            }
            else
            {
                return Json(new { Success = false, Message = "原密码密码不一致" });
            }


        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult RestPwd(int id)
        {

            User b = UserService.GetSelec(id);
            if (b.Pwd == "e10adc3949ba59abbe56e057f20f883e")
            {
                return Json(new { Success = true, Message = "修改成功,原密码：123456" });
            }
            else
            {
                int a = UserService.GetRestPwd(id);
                if (a > 0)
                {
                    return Json(new { Success = true, Message = "修改成功,原密码：123456" });
                }
                else
                {
                    return Json(new { Success = false, Message = "修改失败" });
                }
            }

        }
    }
}